package com.author.surface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.font.TextAttribute;
import java.io.IOException;
import java.text.AttributedCharacterIterator;
import java.text.AttributedString;
import java.text.AttributedCharacterIterator.Attribute;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.border.BevelBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyleConstants;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;

import com.author.document.style.StyleChangeNotify;

public final class OrderedList extends JButton implements ActionListener {
	/** X and Y coordinates of the visible lines */
	private int					x1, x2;
	private int					y1, y2;

	/** Display properties of the button */
	private static final int	OL_BUTTON_WIDTH		= 32;
	private static final int	OL_BUTTON_HEIGHT	= 32;
	private static final int	OL_BUTTON_FONT_SIZE	= 24;

	/** Font used on the Ordered List button */
	private Font				olButtonFont		= new Font(fontName,
															Font.PLAIN,
															OL_BUTTON_FONT_SIZE);
	private static final String	fontName			= "Monospaced";

	/** String to insert an ordered list */
	public static final String	ORDERED_LIST_HTML	= "<HTML><BODY><OL><LI></LI></OL></BODY></HTML>";

	public OrderedList() {
		// Set size of the ORDERED LIST button
		setSize(new Dimension(OL_BUTTON_WIDTH, OL_BUTTON_HEIGHT));
		setPreferredSize(getSize());
		setEnabled(true);

		// Set border of the ORDERED LIST button
		setBorder(new BevelBorder(BevelBorder.LOWERED));
		setBorderPainted(false);

		// Set visible properties
		setFont(olButtonFont);
		setToolTipText("Ordered List");
		setFocusable(false);
		setVisible(true);

		// Make numbered list item insertion button responsive to user click
		addActionListener(this);
	}

	public void paint(Graphics g) {
		super.paint(g);
		Graphics2D g2d = (Graphics2D) g;
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);

		// draw circles and lines indicating that the button is to be used for
		// creating and unordered list
		g2d.setColor(Color.BLACK);

		AttributedString text1 = new AttributedString("1.");
		text1.addAttribute(TextAttribute.FONT, new Font("Monospaced",
				Font.PLAIN, 12));
		text1.addAttribute(TextAttribute.SIZE, 12);

		x1 = 3;
		y1 = 10;
		g2d.drawString(text1.getIterator(), x1, y1);
		x1 = 15;
		y1 = 10;
		x2 = 30;
		y2 = 10;
		g2d.drawLine(x1, y1, x2, y2);

		AttributedString text2 = new AttributedString("2.");
		text2.addAttribute(TextAttribute.FONT, new Font("Monospaced",
				Font.PLAIN, 12));
		text2.addAttribute(TextAttribute.SIZE, 12);

		x1 = 3;
		y1 = 21;
		g2d.drawString(text2.getIterator(), x1, y1);
		x1 = 15;
		y1 = 21;
		x2 = 30;
		y2 = 21;
		g2d.drawLine(x1, y1, x2, y2);

		AttributedString text3 = new AttributedString("3.");
		text3.addAttribute(TextAttribute.FONT, new Font("Monospaced",
				Font.PLAIN, 12));
		text3.addAttribute(TextAttribute.SIZE, 12);

		x1 = 3;
		y1 = 31;
		g2d.drawString(text3.getIterator(), x1, y1);
		x1 = 15;
		y1 = 31;
		x2 = 30;
		y2 = 31;
		g2d.drawLine(x1, y1, x2, y2);
	}

	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae == null) return;
		HTMLDocument doc = (HTMLDocument) TextPane.getTextPaneDocument();
		HTMLEditorKit kit = (HTMLEditorKit) TextPane.getTextPaneEditorKit();

		try {
			int pos = TextPane.getDot();
			kit.insertHTML(doc, pos, ORDERED_LIST_HTML, 1, 1, null);
		} catch (BadLocationException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
